﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using WMSWebApi.Models;
using WMSWebApi.Implement;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace WMSWebApi.Controllers
{
    [Route("api/[controller][action]")]
    [ApiController]
    public class TaskController : ControllerBase
    {
        ImpTask imp = new ImpTask();
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<string> Add([FromBody]object value)
        {
            string query = value.ToString().Replace("\r", "").Replace("\n", "");
            TasksTable tasks = JsonConvert.DeserializeObject<TasksTable>(query);
           return imp.Add(tasks) >=1?"成功":"失败";
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult<string> Delete(int id)
        {
            return imp.Delete(id)>=1?"删除成功":"添加删除";
        }
        /// <summary>
        /// 工位名获取任务
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public  IEnumerable<TasksTable> Get([FromBody]object value)
        {
            string query = value.ToString().Replace("\r", "").Replace("\n", "");
            TaskURL tasks = JsonConvert.DeserializeObject<TaskURL>(query);
            return imp.Get(tasks.StationName,tasks.TaskType);
        }
        /// <summary>
        /// 获取所有任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IEnumerable<TasksTable> GetAll() 
        {
            return imp.GetAll();
        }
        /// <summary>
        /// 任务修改
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPut]
        public ActionResult<string> Update(TasksTable item) 
        {
           return imp.Update(item)>=1?"修改成功":"修改失败";
        }  
    }
}
